package com.tjmc.tiomsresource.service.impl;

import com.google.common.collect.Lists;
import com.tjmc.tiomscore.dao.*;
import com.tjmc.tiomscore.model.*;
import com.tjmc.tiomsresource.parser.CMTnlParser;
import com.tjmc.tiomsresource.service.CMPrtService;
import com.tjmc.tiomsresource.service.CMTnlService;
import com.tjmc.tiomsresource.utils.Constants;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Luc
 * @version 1.0
 * @date 2020-05-20 15:41
 */
@Service("cmTnlService")
public class CMTnlServiceImpl extends CMSuperServiceImpl<TranPtnHwTnl> implements CMTnlService {

    @Autowired
    private CMTnlParser cmTnlParser;
    @Autowired
    private TranPtnHwTnlMapper tranPtnHwTnlMapper;
    @Autowired
    private TiTnlMapper tiTnlMapper;
    @Autowired
    private TiTnlLineMapper tiTnlLineMapper;
    @Autowired
    private TiTnlPointMapper tiTnlPointMapper;
    @Autowired
    private TranPtnTnlMapper tranPtnTnlMapper;
    @Autowired
    private TranPtnLbsMapper tranPtnLbsMapper;
    @Autowired
    private TranPtnTplMapper tranPtnTplMapper;

    @Autowired
    private TiNelMapper tiNelMapper;
    @Autowired
    private TiPortMapper tiPortMapper;
    @Autowired
    private CMPrtService cmPrtService;

    @Override
    public CMTnlParser getParser() {
        return cmTnlParser;
    }

    @Override
    public TranPtnHwTnlMapper getMapper() {
        return tranPtnHwTnlMapper;
    }

    @Override
    public boolean saveTranPtnHwDataToDB(List<File> xmlFiles) {
        return this.saveTranPtnHwDataToDB(xmlFiles, "TNL");
    }

    @Override
    public void buildTiTnlToDB() {
        tiTnlMapper.truncateTable();
        List<TranPtnTnl> tranPtnTnlList = tranPtnTnlMapper.selectByExample(null);
        TiNelExample tiNelExample;
        TiPortExample tiPortExample;
        List<TiTnl> tiTnlList = new ArrayList<>();
        for(TranPtnTnl tranPtnTnl : tranPtnTnlList) {
            TiTnl tiTnl = new TiTnl();
            tiNelExample = new TiNelExample();
            tiNelExample.createCriteria().andRmuidEqualTo(tranPtnTnl.getAendnermuid());
            List<TiNel> tiNels = tiNelMapper.selectByExample(tiNelExample);
            if(null == tiNels || tiNels.size() != 1) {
                tiTnl.setAnelId((long) 0);
            } else {
                tiTnl.setAnelId(tiNels.get(0).getId());
            }
            tiNelExample = new TiNelExample();
            tiNelExample.createCriteria().andRmuidEqualTo(tranPtnTnl.getZendnermuid());
            tiNels = tiNelMapper.selectByExample(tiNelExample);
            if(null == tiNels || tiNels.size() != 1) {
                tiTnl.setZnelId((long) 0);
            } else {
                tiTnl.setZnelId(tiNels.get(0).getId());
            }
            tiPortExample = new TiPortExample();
            tiPortExample.createCriteria().andRmuidEqualTo(tranPtnTnl.getAendportrmuid());
            List<TiPort> tiPorts = tiPortMapper.selectByExample(tiPortExample);
            if(null == tiPorts || tiPorts.size() != 1) {
                tiTnl.setAportId((long) 0);
            } else {
                tiTnl.setAportId(tiNels.get(0).getId());
            }
            tiPortExample = new TiPortExample();
            tiPortExample.createCriteria().andRmuidEqualTo(tranPtnTnl.getZendportrmuid());
            tiPorts = tiPortMapper.selectByExample(tiPortExample);
            if(null == tiPorts || tiPorts.size() != 1) {
                tiTnl.setZportId((long) 0);
            } else {
                tiTnl.setZportId(tiPorts.get(0).getId());
            }
            tiTnl.setTunnelrmuid(tranPtnTnl.getRmuid());
            tiTnl.setNativename(tranPtnTnl.getNativename());
            tiTnl.setDirection(tranPtnTnl.getDirection());
            tiTnl.setActivestate(tranPtnTnl.getActivestate());
            tiTnl.setCir(tranPtnTnl.getCir());
            tiTnl.setPir(tranPtnTnl.getPir());
            tiTnl.setIsoverlay(tranPtnTnl.getIsoverlay());
            tiTnl.setTranPtnTnlId(tranPtnTnl.getId());
            tiTnlList.add(tiTnl);
        }
        if(tiTnlList.size() != 0) {
            List<List<TiTnl>> lists = Lists.partition(tiTnlList, Constants.BATCH_SPLIT_SIZE);
            for(List<TiTnl> tiTnls : lists) {
                tiTnlMapper.insertBatch(tiTnls);
            }
        }

        this.buildTiTnlPointToDB();
        this.buildTiTnlLineToDB();
    }

    private void buildTiTnlPointToDB() {
        logger.info("tnl point 同步开始");
        tiTnlPointMapper.truncateTable();
        List<TranPtnLbs> tranPtnLbsList = tranPtnLbsMapper.selectByExample(null);
        TiNelExample tiNelExample;
        TiPortExample tiPortExample;
        TiTnlExample tiTnlExample;
        List<TiTnlPoint> tiTnlPointList = new ArrayList<>();
        for(TranPtnLbs tranPtnLbs : tranPtnLbsList) {
            tiNelExample = new TiNelExample();
            tiNelExample.createCriteria().andRmuidEqualTo(tranPtnLbs.getNermuid());
            List<TiNel> tiNels = tiNelMapper.selectByExample(tiNelExample);
            if(null == tiNels || tiNels.size() != 1) {
                logger.info("tranPtnLbs.getNermuid() 查询为空: " + tranPtnLbs.getNermuid() + ", " + tranPtnLbs.getId());
                continue;
            }
            TiNel nel = tiNels.get(0);
            tiTnlExample = new TiTnlExample();
            tiTnlExample.createCriteria().andTunnelrmuidEqualTo(tranPtnLbs.getTunnelrmuid());
            List<TiTnl> tiTnlList = tiTnlMapper.selectByExample(tiTnlExample);
            if(null == tiTnlList || tiTnlList.size() != 1) {
                logger.info("tranPtnLbs.getTunnelrmuid() 查询为空: " + tranPtnLbs.getTunnelrmuid() + ", " + tranPtnLbs.getId());
                continue;
            }
            TiTnl tiTnl = tiTnlList.get(0);
            if(null != tranPtnLbs.getAendportrmuid() && !"--".equals(tranPtnLbs.getAendportrmuid())) {
                tiPortExample = new TiPortExample();
                tiPortExample.createCriteria().andRmuidEqualTo(tranPtnLbs.getAendportrmuid());
                List<TiPort> tiPorts = tiPortMapper.selectByExample(tiPortExample);
                TiPort tiPort = null;
                if(null == tiPorts || tiPorts.size() == 0) {
//                    logger.info("tranPtnLbs.getAendportrmuid() 查询为空: " + tranPtnLbs.getAendportrmuid() + ", " + tranPtnLbs.getId());
                    tiPort = cmPrtService.createPortFromTranPtnHwPrt(tranPtnLbs.getAendportrmuid());
                } else {
                    tiPort = tiPorts.get(0);
                }
                TiTnlPoint apoint = new TiTnlPoint();
                apoint.setTiTnlId(tiTnl.getId());
                apoint.setTunnelrmuid(tranPtnLbs.getTunnelrmuid());
                apoint.setTiNelId(nel.getId());
                apoint.setTiPortId(null!=tiPort?tiPort.getId():0);
                apoint.setOrdernum((Integer.parseInt(tranPtnLbs.getRoutingno()) - 1) * 2);
                tiTnlPointList.add(apoint);
            }

            if(null != tranPtnLbs.getZendportrmuid() && !"--".equals(tranPtnLbs.getZendportrmuid())) {
                tiPortExample = new TiPortExample();
                tiPortExample.createCriteria().andRmuidEqualTo(tranPtnLbs.getZendportrmuid());
                List<TiPort> tiPorts = tiPortMapper.selectByExample(tiPortExample);
                TiPort tiPort = null;
                if(null == tiPorts || tiPorts.size() == 0) {
//                    logger.info("tranPtnLbs.getZendportrmuid() 查询为空: " + tranPtnLbs.getZendportrmuid() + ", " + tranPtnLbs.getId());
                    tiPort = cmPrtService.createPortFromTranPtnHwPrt(tranPtnLbs.getZendportrmuid());
                } else {
                    tiPort = tiPorts.get(0);
                }
                TiTnlPoint zpoint = new TiTnlPoint();
                zpoint.setTiTnlId(tiTnl.getId());
                zpoint.setTunnelrmuid(tranPtnLbs.getTunnelrmuid());
                zpoint.setTiNelId(nel.getId());
                zpoint.setTiPortId(null!=tiPort?tiPort.getId():0);
                zpoint.setOrdernum((Integer.parseInt(tranPtnLbs.getRoutingno()) - 1) * 2 + 1);
                tiTnlPointList.add(zpoint);
            }
        }
        if(tiTnlPointList.size() != 0) {
            List<List<TiTnlPoint>> lists = Lists.partition(tiTnlPointList, Constants.BATCH_SPLIT_SIZE);
            for(List<TiTnlPoint> tiTnlPoints : lists) {
                tiTnlPointMapper.insertBatch(tiTnlPoints);
            }
        }
    }

    private void buildTiTnlLineToDB() {
        logger.info("tnl line 同步开始");
        tiTnlLineMapper.truncateTable();
        List<TiTnl> tiTnls = tiTnlMapper.selectByExample(null);
        TranPtnLbsExample tranPtnLbsExample;
        List<TiTnlLine> tiTnlLineList = new ArrayList<>();
        for(TiTnl tiTnl : tiTnls) {
            tranPtnLbsExample = new TranPtnLbsExample();
            tranPtnLbsExample.createCriteria().andTunnelrmuidEqualTo(tiTnl.getTunnelrmuid());
            tranPtnLbsExample.setOrderByClause("routingno");
            List<TranPtnLbs> tranPtnLbsList = tranPtnLbsMapper.selectByExample(tranPtnLbsExample);
            if(null == tranPtnLbsList) {
                continue;
            }
            for(int i=0; i<tranPtnLbsList.size()-1; i++) {
                TiTnlLine tiTnlLine = new TiTnlLine();
                tiTnlLine.setTiTnlId(tiTnl.getId());
                tiTnlLine.setTunnelrmuid(tiTnl.getTunnelrmuid());
                TranPtnLbs current = tranPtnLbsList.get(i);
                TranPtnLbs next = tranPtnLbsList.get(i+1);
                Map<String, String> params = new HashMap<>();
                String aendnermuid = current.getNermuid();
                String zendnermuid = next.getNermuid();
                String aendportrmuid = current.getZendportrmuid();
                String zendportrmuid = next.getAendportrmuid();

                String MPLS = current.getMpls();
                if (StringUtils.isEmpty(MPLS)) {
                    MPLS = next.getMpls();
                }
                List<TranPtnTpl> tranPtnTplList = tranPtnTplMapper.selectAZTpl(aendnermuid, zendnermuid, aendportrmuid, zendportrmuid);
                if(null == tranPtnTplList || tranPtnTplList.size() == 0 || StringUtils.isEmpty(MPLS)) {
                    continue;
                }
                tiTnlLine.setAnelId((long) 0);
                TiNelExample tiNelExample = new TiNelExample();
                tiNelExample.createCriteria().andRmuidEqualTo(aendnermuid);
                List<TiNel> tiNelList = tiNelMapper.selectByExample(tiNelExample);
                if(null != tiNelList && tiNelList.size() > 0) {
                    tiTnlLine.setAnelId(tiNelList.get(0).getId());
                    TiTnlPointExample tiTnlPointExample = new TiTnlPointExample();
                    tiTnlPointExample.createCriteria().andTiNelIdEqualTo(tiNelList.get(0).getId());
                    List<TiTnlPoint> tiTnlPointList = tiTnlPointMapper.selectByExample(tiTnlPointExample);
                    if(null != tiTnlPointList && tiTnlPointList.size() > 0) {
                        tiTnlLine.setApointId(tiTnlPointList.get(0).getId());
                    }
                }
                tiTnlLine.setBnelId((long) 0);
                tiNelExample = new TiNelExample();
                tiNelExample.createCriteria().andRmuidEqualTo(zendnermuid);
                tiNelList = tiNelMapper.selectByExample(tiNelExample);
                if(null != tiNelList && tiNelList.size() > 0) {
                    tiTnlLine.setBnelId(tiNelList.get(0).getId());
                    TiTnlPointExample tiTnlPointExample = new TiTnlPointExample();
                    tiTnlPointExample.createCriteria().andTiNelIdEqualTo(tiNelList.get(0).getId());
                    List<TiTnlPoint> tiTnlPointList = tiTnlPointMapper.selectByExample(tiTnlPointExample);
                    if(null != tiTnlPointList && tiTnlPointList.size() > 0) {
                        tiTnlLine.setBpointId(tiTnlPointList.get(0).getId());
                    }
                }
                tiTnlLine.setOrdernum(i);
                tiTnlLineList.add(tiTnlLine);
            }
        }
        if(tiTnlLineList.size() != 0) {
            List<List<TiTnlLine>> lists = Lists.partition(tiTnlLineList, Constants.BATCH_SPLIT_SIZE);
            for(List<TiTnlLine> tiTnlLines : lists) {
                tiTnlLineMapper.insertBatch(tiTnlLines);
            }
        }

    }
}
